package seven.test_7_19;

public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode cur1 = pHead1;
        ListNode cur2 = pHead2;
        int len1 = 0;
        int len2 = 0;
        while (cur1 != null) {
            cur1 = cur1.next;
            len1++;
        }
        while (cur2 != null) {
            cur2 = cur2.next;
            len2++;
        }
        if (len1 > len2) {
            int i = len1 - len2;
            while (i > 0) {
                pHead1 = pHead1.next;
                i--;
            }
            while (pHead1 != null && pHead2 != null && pHead2 != pHead1) {
                pHead1 = pHead1.next;
                pHead2 = pHead2.next;
            }
        } else {
            int i = len2 - len1;
            while (i > 0) {
                pHead2 = pHead2.next;
                i--;
            }
            while (pHead1 != null && pHead2 != null && pHead2 != pHead1) {
                pHead1 = pHead1.next;
                pHead2 = pHead2.next;
            }
        }
        return pHead1;
    }
}
